<template>
	<view>
		<u-popup  isbg="transparent" :mask-close-able="false" :closeable="false" mode="center" v-model="updata">
			<view style="position: relative;">
				<image mode="widthFix" style="width: 600rpx;" src="./upgrade1.png"></image>
				<view class="flex-ju-c">
					<view @click="doUpData" class="rgb flex-ju-c radius28" style="position: absolute;width: 400rpx;height: 90rpx;bottom: 40rpx;z-index: 5;">
						{{progress===500?'下载失败，请重试':progress===0?'立即更新':('正在更新...'+progress+'%')}}
					</view>
				</view>
			</view>
			<!-- <view @click="tabhide" class="u-flex" style="justify-content: center;margin-top:20rpx;">
				<u-icon name="close-circle" size="50" color="#ccc"></u-icon>
			</view> -->
		</u-popup>
	</view>
	
</template>

<script>
	export default {
		data () {
			return {
				forceable:true,
				version:"",
				content:[],
				androidUrl:"",
				iosUrl:"",
				visible: false,
				progress: 0,
				flat:false,
				appstore:"",
				os_type:"",
				package_type:"",
				updata:false
			}
		},
		
		mounted () {
			this.getversion()
		},
		methods: {
			tabhide(){
				this.updata=false
			},
			getversion(){
				
				// #ifdef APP-PLUS
				let version = uni.getStorageSync('version')?uni.getStorageSync('version'):plus.runtime.version
				let type = plus.os.name.toLowerCase() =='ios'?1:2
				// #endif
				
				
				// #ifdef H5
				 let version = '1.0.0'
				 let type = 1
				// #endif
				 this.$http('v1.getAppVersion', {
				 	type,
				 	num:version
				 }).then(res=>{
					if(res.data.is_has_update == 1){
						var prevVersion = this.fixZeroEnd(uni.getStorageSync('version'), 6).replace(/\./g,'')
						var nextVersion = this.fixZeroEnd(res.data.new_version.num, 6).replace(/\./g,'')
						if (Number(prevVersion) < Number(nextVersion)) {
							this.package_type = res.data.new_version.url.split('.')[1]
							this.androidUrl = res.data.new_version.url
							
							this.os_type = res.data.new_version.type==1?'ios':'andios'
							this.appstore = res.data.new_version.url
							this.content = res.data.new_version.name
							
							this.version = res.data.new_version.num
							uni.hideTabBar()
							this.updata=true
						}
					}else{
						this.updata=false
					}
				})
				
			},
			fixZeroEnd (str,n) {
				return (str + Array(n).join(0)).slice(0, n)
			},
			doUpData() {
				var prevVersion = this.fixZeroEnd(plus.runtime.version, 6).replace(/\./g,'')
				// if (Number(prevVersion) <= 999) {
				// 	if(this.os_type == 'ios'){
				// 		plus.runtime.openURL(this.appstore)
				// 	}else{
				// 		plus.runtime.openURL(this.androidUrl, err=>{
				// 					this.openURL(this.androidUrl)
				// 		},'com.android.browser')
				// 	}
				// 	return
				// }else{
					
				
					
					if(this.package_type == 'wgt'){
						 
					}else{
						if(this.os_type == 'ios'){
							plus.runtime.openURL(this.appstore)
						}else{
							plus.runtime.openURL(this.androidUrl, err=>{
									this.openURL(this.androidUrl)
						},'com.android.browser')
						}
						return
					}
					
				//}
				
				if (this.flat) {
					return
				}
				
				this.flat = true
				// 整包 
				var loadflag = false
				var _this = this
				var osname = plus.os.name
				
					uni.showLoading({
						mask:true
					})
					// 安卓打开网页下载
					
					//plus.runtime.openURL(this.downloadUrl_android);
					var downloadTask = uni.downloadFile({//执行下载
						url: _this.$IMG_URL + _this.androidUrl, //下载地址
						success: downloadResult => {//下载成功
							if (downloadResult.statusCode === 200) {  
							plus.runtime.install(downloadResult.tempFilePath, {  
								force: false  
							}, function(e) {  
								uni.setStorageSync('version',_this.version)
								_this.visible = false
								_this.updata=false
								setTimeout(()=>{
									plus.runtime.restart();
									uni.hideLoading()
								},100)
							}, function(e) {  
								uni.showModal({
									content:JSON.stringify(e)
								})
							});  
						}  
						},
						complete (e) {
							
						}
					})
					
					setTimeout(() => {
						if (!loadflag) {
							this.progress = 500
							uni.hideLoading()
							this.flat = false
						}
					}, 10000)
					downloadTask.onProgressUpdate(res => {
						loadflag = true
						if (res && res.progress) {
							this.progress = res.progress
						}
					})
				
			},
			close () {
				uni.showTabBar()
				this.visible = false
				this.updata = false
			}
		}
	}
</script>

<style scoped lang="scss">
	.rgb{
		color:#fff;
		font-size: 28rpx;
		background-image: url($IMG_URL+'/uploads/20230609/08ceb59314776ff0e9e6a101482709d1.png');
		background-size:100%;
		background-repeat:no-repeat;
		
	}
.cu-modal {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 1110;
	opacity: 0;
	outline: 0;
	text-align: center;
	-ms-transform: scale(1.185);
	transform: scale(1.185);
	backface-visibility: hidden;
	perspective: 2000upx;
	background: rgba(0, 0, 0, 0.6);
	transition: all 0.3s ease-in-out 0s;
	pointer-events: none;
}

.cu-modal::before {
	content: "\200B";
	display: inline-block;
	height: 100%;
	vertical-align: middle;
}

.cu-modal.show {
	opacity: 1;
	transition-duration: 0.3s;
	-ms-transform: scale(1);
	transform: scale(1);
	overflow-x: hidden;
	overflow-y: auto;
	pointer-events: auto;
}

.cu-dialog {
	position: relative;
	display: inline-block;
	vertical-align: middle;
	margin-left: auto;
	margin-right: auto;
	width: 680upx;
	max-width: 100%;
	background-color: #f8f8f8;
	border-radius: 10upx;
	overflow: hidden;
}
.cu-modal .cu-dialog>.cu-bar:first-child .action{
  min-width: 100rpx;
  margin-right: 0;
  min-height: 100rpx;
}
/* .upgrade { position: relative; min-height: 480rpx; overflow: visible; width: 550rpx; padding-top: 320rpx; background: url(./upgrade1.png) center 0 / 550rpx 504rpx no-repeat;} */
.upgrade image { position: absolute; top: -130rpx; right: 0; width: 550rpx; height: 504rpx;}
.version { font-size: 0; z-index:99999;}
.version .padding { font-size: 28rpx;}
.version .des { text-align: left; line-height: 36rpx; padding: 20rpx; font-size: 28rpx; line-height: 48rpx; color: #0B0A12;}
.updatebtn { background: linear-gradient(90deg, #1A86FF, #00B2FF); color: #fff; line-height: 80rpx; height: 80rpx; text-align: center; border-radius: 40rpx; font-size: 28rpx; margin: 0 auto 20rpx;}
.updatebtn_fail { background: #EE1600;}
.dialog_close { margin-bottom: 20rpx; color: #000; font-size: 48rpx;}
.bg-white { background-color: #fff;}
.padding-lr { padding: 0 30rpx;}
.padding-top { padding-top: 30rpx;}
</style>